﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Workflow.Alerts.Sublayouts.ProfileSettings
{
    public partial class PasswordChange : BaseControl, IUserView
    {
        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            PresentationManager.RegisterView(typeof(UserDetailsPresenter), this, new WebSessionProvider());
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.Page.IsValid && OnPasswordUpdate != null)
            {
                OnPasswordUpdate();
                (Session[WorkflowConstants.CurrentUser] as User).Password = txtNewPassword.Text;
                lblStatus.Visible = true;
                lblStatus.Text = "Your password has been changed";
            }
        }

        protected void cvOldPassword_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = (txtOldPassword.Text == (Session[WorkflowConstants.CurrentUser] as User).Password);
        }
        #endregion

        #region IUserView Members

        String IUserView.NewPassword
        {
            get { return txtNewPassword.Text; }
        }

        String IUserView.OldPassword
        {
            get { return txtOldPassword.Text; }
        }

        public event IUserPasswordUpdate OnPasswordUpdate;
        public event IUserPasswordUpdateById OnPasswordUpdateById;

        #endregion
    }
}